import axios from "axios";

import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'

import { useUserStore } from "@/stores/UserStore";
// 创建实例，并配置 基地址
const httpInstance = axios.create({
  // baseURL: 'http://pcapi-xiaotuxian-front-devtest.itheima.net',
  baseURL: 'http://localhost:3000',
  timeout: 5000
});

// 拦截器
httpInstance.interceptors.request.use(config => {
  // 携带 token
  const userStore = useUserStore();
  const token = userStore.userInfo?.token;
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
}, e => Promise.reject(e));

httpInstance.interceptors.response.use(res => res.data, e => {
  console.log(e);
  // 同一错误提示
  ElMessage({
    type: 'error',
    message: e.message
  })
  return Promise.reject(e)
})

export default httpInstance;